package com.woniu108.evaluate.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.evaluate.dto.ProductScoreDto;
import com.woniu108.evaluate.dto.StoreScoreDto;
import com.woniu108.evaluate.form.StoreScoreAddForm;
import com.woniu108.evaluate.model.StoreScore;
import com.woniu108.evaluate.param.StoreScoreAddParam;
import com.woniu108.evaluate.service.StoreScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 门店评分表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/storeScore")
@Api(tags = "门店评分接口")
public class StoreScoreController {

    @Resource
    private StoreScoreService storeScoreService;
    @Resource
    private RedisUtil redisUtil;

    public String getUserIdByToken(String token){
        String refreshToken = redisUtil.getString(token);
        return JWTUtil.parseUserToken(refreshToken).get("userId").toString();
    }

    /**
     * 创建门店的评分信息
     * @param storeId 商品编号
     * @return boolean值
     */
    @GetMapping("/createStoreScoreByStoreId/{storeId}.api")
    @ApiOperation("创建门店的评分信息")
    @AdminHistoryLog
    public Result<Boolean> createStoreScoreByStoreId(@PathVariable("storeId")@ApiParam("门店编号") Integer storeId){
        Boolean aBoolean=storeScoreService.addStoreScoreByStoreId(storeId);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("创建成功");
    }

    /**
     * 为门店评分（用户）
     * @param form 服务订单编号 用户打出评分
     * @param token 登录的userId
     * @return Boolean值
     */
    @PostMapping("/addScore")
    @ApiOperation("为门店评分（用户）")
    public Result<Boolean> addScore(@RequestBody StoreScoreAddForm form, @RequestBody String token){
        String userId = getUserIdByToken(token);

        StoreScoreAddParam param = BeanUtil.toBean(form, StoreScoreAddParam.class);
        param.setUserId(userId);
        Boolean aBoolean=storeScoreService.addScore(param);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("评分成功");
    }

    /**
     * 查询某门店的评分
     * @param storeId 门店id
     * @return 查询某门店的评分信息
     */
    @GetMapping("/queryStoreScore/{storeId}.c")
    @ApiOperation("查询某门店的评分信息")
    public Result<StoreScoreDto> queryStoreScore(@PathVariable("storeId") @ApiParam("门店id") Integer storeId){
        StoreScoreDto storeScoreDto = storeScoreService.selectStoreScoreById(storeId);
        return Result.getResult(StoreScoreDto.class).setData(storeScoreDto).setMsg("查询成功");
    }

    /**
     * 门店删除时，删除其评分信息
     * @param storeId 门店id
     * @return 布尔值
     */
    @GetMapping("/delStoreScore/{storeId}.api")
    @ApiOperation("门店删除时，删除其评分信息")
    @AdminHistoryLog
    public Result<Boolean> delStoreScore(@PathVariable("storeId")@ApiParam("门店id")Integer storeId){
        Boolean aBoolean=storeScoreService.delStoreScore(storeId);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("删除评价记录成功");
    }

    /**
     * 查询所有门店评分
     * @return productScoreDtos
     */
    @GetMapping("/queryAllStoreScore.c")
    @ApiOperation("查询所有商品评分")
    public Result<List<StoreScoreDto>> queryAllProductScore(){
        List<StoreScoreDto> storeScoreDtos=storeScoreService.selectAllStoreScore();
        return Result.newListInstance(StoreScoreDto.class).setData(storeScoreDtos).setMsg("查询所有商品评分");
    }
}

